package com.controller;

import com.dto.excution.ListExcution;
import com.enums.ListEnum;
import com.service.AcService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/**
 * Created by Administrator on 2019/1/8 0008.
 */
@RequestMapping("/ac")
@RestController
public class AcController {
    @Autowired
    private AcService service;

    public static void main(String[] args) throws IOException {
        Process process = Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId" });
        process.getOutputStream().close();
        Scanner sc = new Scanner(process.getInputStream());
        String property = sc.next();
        String serial = sc.next();
        System.out.println(property + ": " + serial);
    }

    @ApiOperation(value = "查用户认证状态")
    @PostMapping(value = "/findUserAcStatus")
    @ResponseBody
    public ListExcution<Void> findUserAcStatus(Integer userId){
        int success = service.findUserAcStatus(userId);
        if(success == 0){
            return new ListExcution<Void>(null, ListEnum.AC);
        }else if(success == -1){
            return new ListExcution<Void>(null, ListEnum.ACFAIL);
        }else if(success == -2){
            return new ListExcution<Void>(null, ListEnum.ACWAIT);
        }else{
            return new ListExcution<Void>(null, ListEnum.SUCCESS);
        }
    }

    @ApiOperation(value = "教师认证")
    @PostMapping(value = "/upImg")
    @ResponseBody
    synchronized public String upImg(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String user = request.getParameter("userId");
        String position = request.getParameter("position");
        String path = null;
        String type = null;
        if (!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
//                    String realPath = request.getRealPath("/lyjjImage/lyjj2/identify/");
                    String realPath = "/usr/lyjjImage/lyjj2/identify/";
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + trueFileName;
                    file.transferTo(new File(path));
                    int i = service.insertDentify(user, "https://www.auriakj.com/lyjjImage/lyjj2/identify/" + trueFileName, position);
                    if (i == 1)
                        return "上传成功";
                    else return "false";
                } else {
                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                    return "error";
                }
            } else {
                System.out.println("文件类型为空");
                return "error";
            }
        } else {
            System.out.println("没有找到相对应的文件");
            return "error";
        }
    }


    @ApiOperation(value = "视频上传")
    @PostMapping(value = "/videoUpload")
    @ResponseBody
    synchronized public String videoUpload(HttpServletRequest request, @RequestParam(value = "files", required = false) MultipartFile files) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String path = null;
        String type = null;
        if (!files.isEmpty()) {
            String fileName = files.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                // 项目在容器中实际发布运行的根路径
//                    String realPath = request.getRealPath("/lyjjImage/lyjj2/identify/");
                String realPath = "F:/videoUp/";
                // 自定义的文件名称
                String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                // 设置存放图片文件的路径
                path = realPath + trueFileName;
                files.transferTo(new File(path));
                    return "上传成功";
//                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
//
//                } else {
//                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
//                    return "error";
//                }
            } else {
                System.out.println("文件类型为空");
                return "error";
            }
        } else {
            System.out.println("没有找到相对应的文件");
            return "error";
        }
    }
}
